<?php
/***********
 *新闻采集类
 *
 *
 */
 class Caiji {
 
	//URL保存文件
	public $_url_save_file;
	
	//title保存文件
	public $_title_save_file;

	//图片保存文件
	public $_img_save_file;

	//图片保存地址
	public $_img_file;

	//文章内容保存文件
	public $_cont_save_file;

	//采集目标的网址
	public $_url;

	//采集规则
	public $_preg;

	//需要采集的数量
	public $_num = 15;
	
	//获取采集到的数据的起始行
	public $_statnum = 0;

	//采集名称
	public $_name;

	//采集类型(主要针对采集的栏目:文章采集，图片，图片文章一起)
	public $_type;


	//返回错误信息
	public $_msg = array("stat" => "采集开始",
						 "end"  => "采集结束", 
		                 "ok"   => "采集顺利进行完成",
		               "aleary" => "今天已经采集", 
		                "no"    => "采集失败",
						"not"   => "采集参数不完整：必须有采集栏目名，保存位置、网址和采集规则"
					);


	/***
	 *构造函数，检查今天是否采集
	 *
	 */
	 public function __construct()	{

		return  "<h2><font color=red>采集程序开始....</font></h2><hr>";
	 }

	 /*****
	  *
	  *默认采集栏目程序
	  *
	  *@param string $url_save_file   URL保存文件
	  *@param string $title_save_file  标题保存文件
	  *@param string $url			   采集页面URL
	  *@param string $preg				采集规则
	  *@param string $name				栏目名
	  *@param int	 $statnum =			采集起始行
	  *@param int	 $num = 15			采集条数
	  *
	  */
	  public function default_caiji($url_save_file,$title_save_file,$url,$preg,$name,$statnum = 0,$num = 15) {

		 $this->_url_save_file		= $url_save_file;
		 $this->_title_save_file	= $title_save_file;
		 $this->_name				= $name;
		 $this->_url				= $url;
		 $this->_preg				= $preg;
		 $this->_statnum			= $statnum;
		 $this->_num				= $num;
		
		 if(is_file($this->_url_save_file) && is_file($this->_title_save_file)) {
				echo $this->_msg['aleary'];
				return false;
		 }
		
		if($this->_name && $this->_url_save_file && $this->_title_save_file && $this->_preg && $this->_url){
				
				$url_content = getUrlContent($this->_url);
				preg_match_all($this->_preg,$url_content,$cai_arr);
				
				//开始写入文件 [1]为url [2]为title 根据采集规则获取
				 
				for($i=$this->_statnum;$i<$this->_num;$i++) {
					if($i+1 == $this->_num) {
						writeFile($this->_url_save_file,$cai_arr[1][$i],"a");
						writeFile($this->_title_save_file,$cai_arr[2][$i],"a");
					}else{
						writeFile($this->_url_save_file,$cai_arr[1][$i]."<br>","a");
						writeFile($this->_title_save_file,$cai_arr[2][$i]."<br>","a");
					
					}
					
				}
				$this->caiji_ok();
				return true;
		   
		   }else{
				$this->caiji_error();
				return false;
		   }
		  
	   }

	/*****
	 *
	 *pho_sina_caiji 新浪图片采集
	 *
	 *@param string $url_save_file   URL保存文件
	 *@param string $title_save_file  标题保存文件
	 *@param string $url            采集页面URL
	 *@param string $preg                采集规则
	 *@param string $name                栏目名
	 *@param string $img_file          图片保存地址
	 *
	 */
	
	public function pho_sina_caiji($url_save_file,$title_save_file,$img_save_file,$img_file,$url,$preg) {
		
		 $this->_url_save_file		= $url_save_file;
		 $this->_title_save_file	= $title_save_file;
		 $this->_img_save_file		= $img_save_file;
		 $this->_img_file			= $img_file;
		 $this->_url				= $url;
		 $this->_preg				= $preg;
		
		 if(is_file($this->_url_save_file) && is_file($this->_title_save_file)) {
				echo $this->_msg['aleary']."<hr>";
				return false;
		 }

		if($this->_img_save_file && $this->_img_file ) {
		
			$url_content = getUrlContent($this->_url);
			preg_match_all($this->_preg,$url_content,$cai_arr);
			
			for($i=$this->_statnum;$i<$this->_num;$i++) {
				
				$caiji[$i] = copyImage($cai_arr[2][$i],array(100,100,0,0),$this->_img_file);
				
				if($i+1 == $this->_num) {
					writeFile($this->_url_save_file,$cai_arr[1][$i],"a");
					writeFile($this->_title_save_file,$cai_arr[3][$i],"a");
					writeFile($this->_img_save_file,$caiji[$i]['url'],"a");
				}else{
					writeFile($this->_url_save_file,$cai_arr[1][$i]."<br>","a");
					writeFile($this->_title_save_file,$cai_arr[3][$i]."<br>","a");
					writeFile($this->_img_save_file,$caiji[$i]['url']."<br>","a");
				}
					
			}

			$this->caiji_ok();
		
			return true;

		}else{
			 
			$this->caiji_error();
			return false;
		}
		
	
	}
	

	/****
	 *凤凰网采集
	 *
	 *@param string $url_save_file   URL保存文件
	 *@param string $title_save_file  标题保存文件
	 *@param string $url            采集页面URL
	 *@param string $preg                采集规则
	 *@param string $name                栏目名
	 *@param string $img_file          图片保存地址
	 *@param string $cont            内容保存地址
	 */
	public function ifeng_caiji($url_save_file,$title_save_file,$img_save_file,$img_file,$cont,$url,$preg) {
		
		 $this->_url_save_file		= $url_save_file;
		 $this->_title_save_file	= $title_save_file;
		 $this->_img_save_file		= $img_save_file;
		 $this->_img_file			= $img_file;
		 $this->_cont_save_file		= $cont;
		 $this->_url				= $url;
		 $this->_preg				= $preg;
		 
		 if(is_file($this->_url_save_file) && is_file($this->_title_save_file)) {
				echo $this->_msg['aleary']."<hr>";
				return false;
		 }

		if($this->_img_save_file && $this->_img_file && $this->_cont_save_file ) {
			
			$url_content = getUrlContent($this->_url);
			preg_match_all($this->_preg,$url_content,$cai_arr);
			
			for($i=$this->_statnum;$i<$this->_num;$i++) {
				
				$caiji[$i] = copyImage($cai_arr[2][$i],array(100,100,0,0),$this->_img_file);
				
				if($i+1 == $this->_num) {
					writeFile($this->_url_save_file,$cai_arr[1][$i],"a");
					writeFile($this->_title_save_file,$cai_arr[7][$i],"a");
					writeFile($this->_cont_save_file,$cai_arr[9][$i],"a");
					writeFile($this->_img_save_file,$caiji[$i]['url'],"a");
				}else{
					writeFile($this->_url_save_file,$cai_arr[1][$i]."<br>","a");
					writeFile($this->_title_save_file,$cai_arr[7][$i]."<br>","a");
					writeFile($this->_cont_save_file,$cai_arr[9][$i]."<br>","a");
					writeFile($this->_img_save_file,$caiji[$i]['url']."<br>","a");
				}
					
			}
			
			$this->caiji_ok();
			return true;
		}else{
			$this->caiji_error();
			return false;
		
		}
		
	}


	/***
	 *
	 *采集成功提示信息
	 */

	 public function caiji_ok() {
		
		echo $this->_name.$this->_msg['ok']."<hr>";
		return true;
	 
	 }


	 /***
	 *
	 *采集失败提示信息
	 */

	 public function caiji_error() {
		
		echo $this->_name.$this->_msg['not']."<hr>";
		return true;
	 
	 }

	 /****
	  *析构函数
	  */
	  public function __destruct() {
	  
		echo "<h2><font color=red>采集程序结束</font></h2>";
	  }
}